package com.tenton.servlet;

import com.tenton.pojo.Book;
import com.tenton.pojo.ShoppingCart;
import com.tenton.service.BookService;
import com.tenton.service.CartService;
import com.tenton.service.Impl.BookServiceImpl;
import com.tenton.service.Impl.CartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Date: 2021/1/20
 * @Author: Tenton
 * @Description:
 */
@WebServlet(name = "InsertCartServlet",urlPatterns = "/insertCartServlet")
public class InsertCartServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式，防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        CartService cartService = new CartServiceImpl();
        BookService bookService = new BookServiceImpl();
        //获取session中的用户Id
        HttpSession session = request.getSession();
        //当前用户Id
        int userId = (int) session.getAttribute("userId");
        //图书Id
        int bookId = Integer.parseInt(request.getParameter("bookId"));
        Book book = bookService.getBook(bookId);
        //图书名称
        String bookName = book.getBookName();
        //查询购物车里所有的数据，来遍历查询cart里面是否有图书和用户对应的数据
        List<ShoppingCart> carts = cartService.listCart();
        //遍历carts查询cart表里面是否含有userId和bookName对应的值，有就执行cart的购买数量加一，没有就执行cart添加
        //判断数据库中是否存在对应cart的数据,true：表示已存在，执行修改操作，false：表示不存在，执行增加操作
        boolean flag = false;
        for (ShoppingCart cart : carts) {
            if (cart.getUserId().equals(userId) && cart.getBookId().equals(bookId)){
                flag = true;
            }
        }
        //用于判断是否增加成功
        Integer result = null;
        if (flag){
            //通过登录Id和图书名称查询购买数量
            int num = cartService.getBuyNum(userId, bookId);
            num += num;
            //图书单价
            BigDecimal price = book.getPrice();
            //总价
            BigDecimal bd = new BigDecimal(num);
            BigDecimal totalPrice = price.multiply(bd);
            ShoppingCart cart = cartService.getCartByUBId(userId, bookId);
            cart.setNum(num);
            cart.setTotalPrice(totalPrice);
            cartService.updateCart(cart);
        }else {
            //新建一个购物车对象，保存需要放入购物车数据
            ShoppingCart newCart = new ShoppingCart(userId,bookId, book.getBookName(), book.getPrice(), 1,book.getPrice());
            //将选择的商品信息增加到数据库
            cartService.insertCart(newCart);
        }
        //响应重定向到listCartServlet，刷新购物车界面
        response.sendRedirect("/listCartServlet");
    }
}
